//index.js
//获取应用实例
var util = require('../../utils/util.js');
var app = getApp();
var today = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 7))).split(' ')[0];
var minday = util.formatTime(new Date()).split(' ')[0];
var maxday = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 62))).split(' ')[0];
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var page = 1;
var list = new Array();
var list1 = new Array();
var list2 = new Array();

Page({
    data: {
        tabs: ["车主", "乘客"],
        activeIndex: 1,
        departure: "从哪里出发？",
        destination: "去哪儿？",
        test:1
    },
    tabClick: function (e) {
        this.setData({
            activeIndex:1
        });
    },
    tabClick1: function (e) {
        this.setData({
            activeIndex:2
        });
    },
    test: function (e) {
        this.setData({
            test:1
        })
    },
    test1: function (e) {
        this.setData({
            test:2
        })
    },
    onShareAppMessage: function () {
        return {
            title: '皮皮虾拼车',
            path: 'pages/index/index'
        }
    },
    // getList: function () {
    //     var that = this;
    //     util.req('/wx/wxOrder/list',
    //         {page: page},
    //         function (data) {
    //             if (data.content.length == 0) {
    //                 that.setData({nomore: true});
    //                 return false;
    //             }
    //             if (page == 1) {
    //                 list = new Array();
    //                 list1 = new Array();
    //                 list2 = new Array();
    //             }
    //             var surp = new Array('', '空位', '人');
    //             data.content.forEach(function (item) {
    //                 var obj = {
    //                     start: item.departure,
    //                     over: item.destination,
    //                     type: that.data.tabs[item.type],
    //                     tp: item.type,
    //                     time: item.time,
    //                     avatarUrl: app.globalData.userInfo.avatarUrl,
    //                     url: '/pages/info/index?id=' + item.id,
    //                     tm: util.getDateDiff(item.time * 1000)
    //                 };
    //                 if (item.type == 1) {
    //                     obj.surplus = item.registrationDate + surp[item.type]
    //                     list1.push(obj);
    //                 } else {
    //                     obj.surplus = item.number + surp[item.type]
    //                     list2.push(obj);
    //                 }
    //                 list.push(obj);
    //             })
    //
    //             that.setData({list: list, list1: list1, list2: list2});
    //             console.log(that.data)
    //         })
    //
    // },
    onPullDownRefresh: function () {
        page = 1;
        // this.getList();
        wx.stopPullDownRefresh();
    },
    onLoad: function () {
        var that = this;
        util.req('/wx/index/getImg', {}, function (data) {
            that.setData({movies: data.content});
        })
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
                    windowHeight: res.windowHeight,
                    windowWidth: res.windowWidth
                });
            }

        });

        wx.getLocation({
            success: function (res) {
                var latitude = res.latitude
                var longitude = res.longitude
                wx.request({
                    url: 'https://api.map.baidu.com/geocoder/v2/?ak=zIOkoO8wWrWA22ObIHPNkCgtLZpkP5lE&location=' + latitude + ',' + longitude + '&output=json&pois=0',
                    data: {},
                    method: 'GET',
                    header: {'Content-Type': 'application/json'},
                    success: function (res) {
                        console.log(res)
                        that.setData({
                            start: res.data.result.addressComponent.city
                        })
                        // that.getList();
                    }
                })
            }
        })
    },
    sexDeparture: function () {
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                that.setData({
                    departure: res.address,
                    startlatitude:res.latitude,
                    startlongitude:res.longitude
                })
            }
        })
    },
    sexDestination: function () {
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                that.setData({
                    destination: res.address,
                    endlatitude:res.latitude,
                    endlongitude:res.longitude
                })
            }
        })
    },
    jumpAdd: function () {
        var that = this
        //判断认证
        util.req("/wx/user/test",{'activeIndex':that.data.activeIndex,'wxUserId':app.globalData.sk},function (result) {
            if (result.content == 1){
                if (that.data.departure == '从哪里出发？') {
                    wx.showToast({
                        title: '请选择出发地',
                        icon: 'error',
                        duration: 2000
                    })
                    return false;
                }
                if (that.data.destination == '去哪儿？') {
                    wx.showToast({
                        title: '请选择目的地',
                        icon: 'error',
                        duration: 2000
                    })
                    return false;
                }
                //1车主，2乘客
                var a = that.data.activeIndex

                //0市内，1跨城
                var b = that.data.test

                //出发地
                var start = that.data.departure
                var startlatitude = that.data.startlatitude
                var startlongitude = that.data.startlongitude
                //目的地
                var end = that.data.destination
                var endlatitude = that.data.endlatitude
                var endlongitude = that.data.endlongitude
                wx.redirectTo({
                    url: '/pages/info/add?activeIndex=' + a + '&start=' + start + '&startlatitude='+startlatitude+'&startlongitude='+startlongitude+
                    '&end=' + end+'&endlatitude='+endlatitude+'&endlongitude='+endlongitude+'&test='+b
                })
            }else if (result.content == 2){//实名认证
                wx.showToast({
                    title: '请先进行实名认证',
                    icon: 'error',
                    duration: 2000
                })
            }else if (result.content == 3){//车主认证
                wx.showToast({
                    title: '请先进行车主认证',
                    icon: 'error',
                    duration: 2000
                })
            }else if (result.content == 4){//资料完善
                wx.showToast({
                    title: '请先完善个人资料',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    },
    // onReachBottom: function () {
    //     if (!this.data.nomore) {
    //         page++;
    //         this.getList();
    //     }
    // }
})
